package com.wk.springboot.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.wk.springboot.mp.ITestUserService;
import com.wk.springboot.mp.TestUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;


@Service
@Slf4j
public class ScheduledServiceImpl implements ScheduledService{

    @Autowired
    ITestUserService iTestUserService;

    public ScheduledServiceImpl(){
        System.out.println("ScheduledServiceImpl 构造方法");
    }


//    @Scheduled(cron = "${task.cron}")
//@Transactional(rollbackFor = Exception.class)
@Override
    public void scheduleGenDailyCheckRecord() {
        generateDailyCheckRecord(100);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void generateDailyCheckRecord(Integer day) {
        log.info("generateDailyCheckRecord1 {}", TransactionSynchronizationManager.getCurrentTransactionName());
        List<TestUser> objects1 = Lists.newArrayList();
        for (int i = 0;i<day;i++){
            TestUser testUser = new TestUser();
            testUser.setId(UUID.randomUUID().toString().replace("-",""));
            testUser.setName("zs");
            objects1.add(testUser);
        }
        TestUser testUser2 = new TestUser();
        testUser2.setId(UUID.randomUUID().toString().replace("-","").substring(0,12));
        testUser2.setName("zss");
        iTestUserService.saveBatch(objects1);
        iTestUserService.save(testUser2);
        log.info("generateDailyCheckRecord2 {}", TransactionSynchronizationManager.getCurrentTransactionName());
        log.info("generateDailyCheckRecord2 {}", TransactionSynchronizationManager.isActualTransactionActive());

        List<TestUser> objects2 = Lists.newArrayList();
        for (int i = 0;i<day;i++){
            TestUser testUser = new TestUser();
            testUser.setId(UUID.randomUUID().toString().replace("-",""));
            testUser.setName("ls");
            objects2.add(testUser);
        }
        try {
            iTestUserService.saveBatchWithCustomer(objects2);
            log.info("generateDailyCheckRecord3 {}", TransactionSynchronizationManager.getCurrentTransactionName());
        } catch (Exception e){
            log.error("{}",e);
//            throw e;
        }

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void mockTransactional() {
        final List<TestUser> userList = this.iTestUserService.list(new QueryWrapper<TestUser>()
                .lambda()
                .eq(TestUser::getType, 1));

        log.info("userList {}",userList);

        updateType();

        final List<Integer> collect = Stream.of(1, 2).collect(Collectors.toList());
        collect.parallelStream().forEach(s->{
            selectType();
        });

    }

    @Transactional(rollbackFor = Exception.class)
    public void selectType() {
        final List<TestUser> list = this.iTestUserService.list(new QueryWrapper<TestUser>()
                .lambda()
                .eq(TestUser::getType, 1));
        log.info("selectType {}",list);
    }

    private void updateType(){

        final boolean update = this.iTestUserService.update(new UpdateWrapper<TestUser>()
                .lambda()
                .set(TestUser::getType, 2)
                .eq(TestUser::getType, 1));
        log.info("updateType {}",update);

    }
}
